package com.itsenpupulai.longlivetest;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import org.json.JSONObject;

/**
 * Created by Administrator on 2017/8/12.
 */

public class SocketData {
    public static String getLoginData(Context context) {

        Context tempContext = context.getApplicationContext();

        try {
            JSONObject obj2 = new JSONObject();
            obj2.put("user_id", "411414");
//			MyLog.e("长连接----》"+ new DbUtil(tempContext).getUserId());
            obj2.put("city_id", "410100");
//			MyLog.e("长连接----》"+ new DbUtil(tempContext).getCityId());
//            String location = new DbUtil(tempContext).getLocationPoint();
            try {
//                if (location == null || location.length() == 0) {
                obj2.put("lat", "113.629552");
                obj2.put("lng", "34.800927");
//                } else {
//                    String[] temparr = location.split(",");
//                    if (temparr.length > 1) {
//                        obj2.put("lat", temparr[0]);
//                        obj2.put("lng", temparr[1]);
//                    } else {
//                        obj2.put("lat", "0");
//                        obj2.put("lng", "0");
//                    }
//                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            obj2.put("device", 4);
            obj2.put("source", getPhoneModel());
//			obj2.put("group_id", "3");
            obj2.put("version", getVersionName(tempContext));
            JSONObject obj = new JSONObject();
            obj.put("data", obj2);
            obj.put("action", "login");
            return obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getLocationData(Context context) {

        Context tempContext = context.getApplicationContext();

        try {
            JSONObject obj2 = new JSONObject();
            obj2.put("user_id", "411414");
//            String location = new DbUtil(tempContext).getLocationPoint();
            try {
//
//                if (location == null || location.length() == 0) {
                obj2.put("lat", "113.629552");
                obj2.put("lng", "34.800927");
//                } else {
//                    String[] temparr = location.split(",");
//                    if (temparr.length > 1) {
//                        obj2.put("lat", temparr[0]);
//                        obj2.put("lng", temparr[1]);
//                    } else {
//                        obj2.put("lat", "0");
//                        obj2.put("lng", "0");
//                    }
//                }
            } catch (Exception e) {
                e.printStackTrace();
                obj2.put("lat", "0");
                obj2.put("lng", "0");
            }

            JSONObject obj = new JSONObject();
            obj.put("data", obj2);
            obj.put("action", "point");
            return obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    // 收到推送时，发送给服务器的回执消息
    public static String getPushToServiceMsg(String jsonStr) {
        try {
            JSONObject obj = new JSONObject(jsonStr);
            JSONObject tempObj = new JSONObject();
            tempObj.put("success", true);
            tempObj.put("request_id", obj.getJSONObject("data").getInt("request_id"));
            obj = new JSONObject();
            obj.put("data", tempObj);
            obj.put("action", "antpush");
            return obj.toString();
        } catch (Throwable e) {
            e.printStackTrace();
            return "";
        }
    }


    //  测试推送 1 接收推送回执
    public static String getReceiptToServiceMsg(String jsonStr) {
//		MyLog.e("推送回执1");
        try {
            JSONObject obj = new JSONObject(jsonStr);
            JSONObject tempObj = new JSONObject();
            tempObj.put("success", "1");
            tempObj.put("request_id", obj.getJSONObject("data").getInt("request_id"));
            obj = new JSONObject();
            obj.put("data", tempObj);
            obj.put("action", "receipt");
            return obj.toString();
        } catch (Throwable e) {
            e.printStackTrace();
            return "";
        }
    }

    //  测试推送 2 弹窗推送回执
    public static String getReceiptToServiceMsg2(String jsonStr) {
//		MyLog.e("推送回执2");
        try {
            JSONObject obj = new JSONObject(jsonStr);
            JSONObject tempObj = new JSONObject();
            tempObj.put("success", "2");
            tempObj.put("request_id", obj.getJSONObject("data").getInt("request_id"));
            obj = new JSONObject();
            obj.put("data", tempObj);
            obj.put("action", "receipt");
            return obj.toString();
        } catch (Throwable e) {
            e.printStackTrace();
            return "";
        }
    }


    /* 获取手机型号 */
    public static String getPhoneModel() {
        try {
            return android.os.Build.MODEL;
        } catch (Throwable e) {
            e.printStackTrace();
            return "";
        }
    }


    /* 获取版本号 */
    public static String getVersionName(Context c) {
        // 获取packagemanager的实例
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo;
        try {
            packInfo = c.getPackageManager().getPackageInfo(c.getPackageName(), 0);
            return packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }

    }
}
